{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy\n",
    "from numpy import arange, sin, exp, pi, diff, floor, asarray\n",
    "from scipy.io import savemat\n",
    "from utils.simulation_utils import add_noise, quantize\n",
    "from utils.gps_l1ca_utils import generate_GPS_L1CA_code\n",
    "from utils.acquisition_utils import coarse_acquire\n",
    "from utils.utils import PSKSignal, sample_sequence\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams.update({'font.size': 20})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T_sim = 4                       # duration of simulated signal (s)\n",
    "fs = 5e6                        # sampling rate (Hz)\n",
    "N = int(T_sim * fs)\n",
    "t = arange(N) / fs\n",
    "\n",
    "f_carrier = 1.57542e9           # L1 carrier frequency (Hz)\n",
    "f_center = 1.57542e9 - 1.25e6   # radio front-end center frequency (Hz)\n",
    "f_inter = f_carrier - f_center  # intermediate frequency (Hz)\n",
    "f_code = 1.023e6                # L1 C/A code rate (chips/s)\n",
    "\n",
    "c = 299792458                   # speed of light (m/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "G_flat = t * 0 - pi / (2 * pi * f_carrier / c)\n",
    "G_step = .1 * (t > T_sim / 2)\n",
    "G_linear = 0.5 * (t > T_sim / 2) * 3 * (t - T_sim / 2)\n",
    "G_quadratic = 0.5 * (t >= T_sim / 2) * 2 * (t - T_sim / 2)**2\n",
    "G_sinusoid = 0.5 * (exp((t - T_sim / 2)) * (t < T_sim / 2) + (t >= T_sim / 2)) * sin(2 * pi * 4 / T_sim * t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(12, 5), dpi=300)\n",
    "ax = fig.add_subplot(111)\n",
    "skip = 1000\n",
    "args = {'linewidth': 3}\n",
    "ax.plot(t[:-1:skip], -1.3+diff(G_flat)[::skip] * fs * f_carrier / c, label='flat', **args)\n",
    "ax.plot(t[:-1:skip], diff(G_step)[::skip] * fs * f_carrier / c, label='impulse', **args)\n",
    "ax.plot(t[:-1:skip], diff(G_linear)[::skip] * fs * f_carrier / c, label='step', **args)\n",
    "ax.plot(t[:-1:skip], diff(G_quadratic)[::skip] * fs * f_carrier / c, label='ramp', **args)\n",
    "ax.plot(t[:-1:skip], diff(G_sinusoid)[::skip] * fs * f_carrier / c, label='growing sinusoid', **args)\n",
    "ax.set_xlim(t[0], t[-1])\n",
    "ax.set_ylim(-40, 60)\n",
    "ax.set_xlabel('Time [s]')\n",
    "ax.set_ylabel('Doppler [Hz]')\n",
    "ax.legend(loc=2, fontsize=16, frameon=False)\n",
    "ax.grid()\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prns = [4, 7, 10, 15, 29, 32]\n",
    "n0s = [100, 1225, 2500, 4560, 2052.7846, 0.5]\n",
    "tau0s = [n0 / fs for n0 in n0s]\n",
    "fds = [1000, -1000, 2200, -3300, -3210, -4001]\n",
    "Gs = [c * tau0 + c / f_carrier * fd * t + G for tau0, fd, G in zip(tau0s, fds, [G_flat, G_step, G_linear, G_quadratic, G_sinusoid, G_flat])]\n",
    "cn0s = [45, 47, 49, 45, 41, 45 - 26 / 6 * t]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "signal_samples = []\n",
    "chips = []\n",
    "for prn, G in zip(prns, Gs):\n",
    "    code_seq = generate_GPS_L1CA_code(prn)\n",
    "    \n",
    "    chip = f_code * G / c\n",
    "    code_samples = exp(1j * pi * code_seq[(floor(t * f_code + chip) % len(code_seq)).astype(int)])\n",
    "    theta = 2 * pi * f_carrier * G / c\n",
    "    samples = code_samples * exp(1j * (2 * pi * f_inter * t + theta))\n",
    "    \n",
    "    chips.append(chip)\n",
    "    signal_samples.append(samples)\n",
    "\n",
    "samples = add_noise(signal_samples, cn0s)\n",
    "samples = quantize(samples, bits=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists('../data'):\n",
    "    os.makedirs('../data')\n",
    "filepath = '../data/sim-RF_GPS-L1CA_5000_1250_complex_{0:02}s.mat'.format(T_sim)\n",
    "savemat(filepath, {'samples': samples, 'chips': asarray(chips), 'prns': prns})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
